﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using System.Text.RegularExpressions;

namespace LanguageStudy
{
    public class WordsHandle
    {
        private ArrayList m_alWords = new ArrayList();
        public WordsHandle(string strName)
        {
            m_strName = strName;
        }

        ~WordsHandle()
        {
            m_alWords.Clear();
        }

        public override string ToString()
        {
            return m_strName + " (" + m_alWords.Count + ")";
        }

        private string m_strName = null;
        public string Name
        {
            get
            {
                return m_strName;
            }
            set
            {
                m_strName = value;
            }
        }

        public ArrayList ImportFile(string strFile)
        {
            ArrayList _alFileLines = new ArrayList();
            try
            {
                StreamReader _srRead = new StreamReader(strFile, Encoding.Default);

                while (!_srRead.EndOfStream)
                    _alFileLines.Add(_srRead.ReadLine());

                _srRead.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Import File", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            m_alWords.Clear();
//             Word _Word;

            DecodeYouDao(_alFileLines);
//             if (_Word != null)
//                 m_alWords.Add(_Word);

            return m_alWords;
        }

        public ArrayList RandomWords(int nNum)
        {
            ArrayList _alWordsCopy = new ArrayList(m_alWords);
            ArrayList _alReturn = new ArrayList();

            Random rand = new Random();

            int _nNum = nNum;
            if (_nNum <= 0)
                _nNum = _alWordsCopy.Count;
            while (_alWordsCopy.Count > 0 && _nNum > 0)
            {
                int _i = rand.Next() % _alWordsCopy.Count;
                _alReturn.Add(_alWordsCopy[_i]);
                _alWordsCopy.RemoveAt(_i);

                _nNum--;
            }

            return _alReturn;
        }

        public ArrayList GetWords()
        {
            return m_alWords;
        }

        private void DecodeYouDao(ArrayList alFileLines)
        {
            int _nID = 0;
            string _strLine;
            string[] _strItem;

            while (_nID < alFileLines.Count)
            {
                _strLine = ((string)alFileLines[_nID++]).Trim();

                if (_strLine == "")
                    continue;

                // 匹配词汇首行
                if(Regex.IsMatch(_strLine, "^\\d+,", RegexOptions.None))
                {
                    // 创建新词汇
                    Word _word = new Word();

                    // 拆分行
                    _strItem = _strLine.Split(new string[] { "  " }, StringSplitOptions.RemoveEmptyEntries);

                    // 单词
                    _word.m_strWord = _strItem[0].Substring(_strItem[0].IndexOf(", ") + 2);
                    
                    // 音标
                    if(_strItem.Length > 1)
                        _word.m_strPhonetic = _strItem[1];

                    // 添加词汇
                    m_alWords.Add(_word);
                }
                else
                {
                    Word.Meaning _Meaning = new Word.Meaning();

                    _strItem = _strLine.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                    if (_strItem.Length == 1)
                    {
                        _Meaning.m_strPartOfSpeech = "";
                        _Meaning.m_strMeaning = _strItem[0];
                    }
                    else
                    {
                        _Meaning.m_strPartOfSpeech = _strItem[0];
                        _Meaning.m_strMeaning = _strItem[1];
                    }

                    if (m_alWords.Count > 0)
                        ((Word)m_alWords[m_alWords.Count - 1]).m_alMeanings.Add(_Meaning);
                }
            }





/*
            // 按空格拆分
            _strArray = strLine.Split(' ');

            // 项数小于2忽略
            if (_strArray.Length < 2)
                return null;

            // 第一项结尾不是","忽略
            if (_strArray[0].Substring(_strArray[0].Length - 1) != ",")
                return null;

            // 第二项为单词开始
            _strWord = _strArray[1];

            // 查看词组，并遇音标跳出
            for (int i = 2; i < _strArray.Length; i++)
            {
                if (_strArray[i] != "")
                {
                    if (_strArray[i].Substring(0, 1) == "[")
                        break;
                    else
                    {
                        _strWord += " ";
                        _strWord += _strArray[i];
                    }
                }
            }

            Word _w = new Word();
            _w.m_strWord = _strWord;

            return _w;*/
        }
    }
}
